package com.hwy.catering.interceptor;

import com.hwy.catering.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.hwy.catering.utils.ConstantPool.TOKEN_KEY;

/**
 * 状态拦截器，判断该用户是否被禁用
 */
public class StopInterceptor implements HandlerInterceptor {

    private StringRedisTemplate stringRedisTemplate;

    public StopInterceptor(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = UserHolder.getUser().getToken();
        Object stop = stringRedisTemplate.opsForHash().get(TOKEN_KEY + token, "stop");
        if (stop.equals("0")){
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
